package com.situ.lession1226.api;

import com.situ.lession1226.model.Rating;
import com.situ.lession1226.service.RatingService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

//对每个电影下面的评论进行展示和增加
@RestController
@RequestMapping(value = "/api/v1/rating", produces = "application/json;charset=utf-8")
public class RatingApi {
    private RatingService RatingService;

    public RatingApi(RatingService RatingService) {
        this.RatingService = RatingService;
    }

//    @GetMapping("/get")
//    public Map<String,Object> get(Integer id){
//        List <Rating> Rating=RatingService.findAll(id);
//
//        if (Rating == null) {
//            return Map.of("success", false);
//        } else {
//            return Map.of("success",true,"Rating",Rating);
//        }
//
//    }
    @PostMapping("/post")
    public Map<String, Object> post(Rating Rating){

        boolean success = RatingService.save(Rating);

//        如果有记录则更新，没有则插入
        if(!RatingService.haveaver(Rating.getNewid())) {
            RatingService.insertaver(Rating.getNewid());
        }else {
            RatingService.aver(Rating.getNewid());
        }
        return Map.of("success", success);
    }
}
